"use strict";

exports.__esModule = true;
exports.AutoPlayVideo = undefined;
exports.getTecentVideo = getTecentVideo;

var _http = require("./http");

var Http = _interopRequireWildcard(_http);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } }

/**
 * 通过vid获取腾讯视频地址
 * 
 * @param {*} vid 
 * @param {*} endFix 
 * @param {*} prefix 
 */
function getTecentVideo(vid) {
    var endFix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "&platform=101001&charge=0&otype=json&defn=hd&defaultfmt=auto";
    var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "https://vv.video.qq.com/getinfo";

    return new Promise(function (resolve, reject) {
        if (!vid) {
            console.error("no vid");
            reject(new Error("no vid"));
        }
        var hdPlatform = endFix;
        Http.get(prefix + "?vids=" + vid + hdPlatform).then(function (res) {
            // console.log(res);
            var JSONDATA = res.replace("QZOutputJson=", "");
            JSONDATA = JSONDATA.slice(0, JSONDATA.length - 1);
            try {
                var videoInfo = JSON.parse(JSONDATA);
                if (videoInfo.msg && videoInfo.msg.indexOf("vid is wrong") > -1) {
                    reject(new Error(videoInfo.msg));
                }
                if (videoInfo.vl && videoInfo.vl.vi) {
                    var videos = videoInfo.vl.vi;
                    var curVideo = videos[0];
                    var fn = curVideo.fn;
                    var fvkey = curVideo.fvkey;
                    var curVideoPlayAddress = "";
                    var playurl = fn + "?vkey=" + fvkey;
                    if (curVideo) {
                        if (curVideo.ul && curVideo.ul.ui) {
                            curVideoPlayAddress = curVideo.ul.ui[0];
                        }
                    }
                    console.log(videoInfo);
                    if (curVideoPlayAddress) {
                        playurl = curVideoPlayAddress.url + playurl;
                        resolve(playurl);
                    } else {
                        reject(new Error("no curVideoPlayAddress"));
                    }
                } else {
                    reject(new Error("no videoInfo"));
                }
            } catch (e) {
                console.error(e);
                reject(e);
            }
        })["catch"](function (e) {
            console.error(e);
            reject(e);
        });
    });
}

/**
 * 检测当前网络情况 如果是wifi就直接播放 否则调用失败回调
 *
 * @constructor
 */
function AutoPlayVideo(_success, failed) {
    wx.getNetworkType({
        success: function success(res) {
            var networkType = res.networkType;
            console.log("cur networkType", networkType);
            if (networkType === "wifi") {
                if (_success) {
                    _success();
                }
            } else {
                if (failed) {
                    failed();
                }
            }
        },
        fail: function fail() {
            if (failed) {
                failed();
            }
        }
    });
}
exports.AutoPlayVideo = AutoPlayVideo;